import logger from "koa-logger";
import json from "koa-json";
import koaBody from "koa-body";
// @ts-ignore
import onerror from "koa-onerror";
import cors from "@koa/cors";
import compress from "koa-compress";
import compose from "koa-compose";
import { KoaApplication } from "@/types/index";

export const middleware = (app: KoaApplication) => {
  onerror(app);

  const middleware = compose([
    compress(), //使用压缩中间件启用 gzip 压缩
    koaBody(), // 解析body
    cors(), //设置允许跨域
    json(), // json
    logger(), // logger 日志
  ]);
  app.use(middleware);
};
